<template>
	<view>
		<view class="content" v-if="prepare">
			<view class="item">
				<view class="title">{{list.questionnaireQuestion.questionnaireSubjectContent}}</view>
				<text class="limit">最多选{{maxNumber}}项</text>
				<view v-for="(op, j) in list.questionnaireOptions" :key="j">
					<view @click="selectOption(j)" :class="ans[j]?'select':'option'">
						{{op.optionContent}}
					</view>
				</view>
			</view>
		</view>
		<view class="footer">
			<button type="primary" @click="submit">
				<text class="iconfont">&#xe612;</text>提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getQuestionnaireDetail,
		respondence
	} from '@/api/questionnaire.js'
	export default {
		data() {
			return {
				qid: '',
				list: [],
				ans: [],
				currentNumber: 0,
				maxNumber: '',
				prepare:false
			}
		},
		methods: {
			getData() {
				uni.showLoading()
				getQuestionnaireDetail({
					questionnaireId: this.qid
				}).then(res => {
					this.maxNumber = Number(res.data.data[0].questionnaire.con)
					this.list = res.data.data[0].list[0]
					this.ans = new Array(this.list.questionnaireOptions.length).fill(false)
					this.prepare = true
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.showModal({
						content: '请求失败'
					})
				})
			},
			selectOption(j) {
				if (this.ans[j]) { //取消选中
					this.ans[j] = false
					this.currentNumber--
				} else { //选中
					if (this.currentNumber == this.maxNumber) { //已经达到最大数目
						uni.showToast({
							icon: 'none',
							title: '最多选' + this.maxNumber + '项'
						})
					} else {
						this.ans[j] = true
						this.currentNumber++
					}
				}
				console.log(this.ans)
			},
			submit() {
				if (this.currentNumber == 0) {
					uni.showToast({
						icon: 'none',
						title: '尚未做答完成'
					})
					return
				}
				uni.showModal({
					content: '您确定要提交？提交之后不可更改',
					cancelText: '我再想想',
					success:(res)=> {
						if (res.confirm) {
							for (let i in this.ans) {
								if (this.ans[i]) {
									let obj = {
										questionnaire: {
											questionnaireId: this.qid
										},
										questionnaireMember: {
											userId: getApp().globalData.id,
											questionnaireId: this.qid,
											answer: 1
										},
										list: []
									}
									obj.list.push({
										questionnaireQuestion: {
											questionnaireQuestionId: this.list.questionnaireQuestion.questionnaireQuestionId,
										},
										questionnaireAnswer: {
											questionnaireId: this.qid,
											questionnaireQuestionId: this.list.questionnaireQuestion.questionnaireQuestionId,
											answer: this.list.questionnaireOptions[i].optionType,
											id: getApp().globalData.id
										}
									})
									respondence(obj).then(res => {
										if (res.data.code == 0) {
											uni.showModal({
												content: '提交成功',
												showCancel: false,
												success() {
													uni.navigateBack()
												}
											})
										}
									})
								}
							}
						}
					}
				})
			}
		},
		onLoad(option) {
			this.qid = option.qid
			this.getData()
		}
	}
</script>

<style>
	page {
		background-color: #FFFAEE;
		background-image: linear-gradient(#FFFAEE, #ffe0c5);
		font-family: 'Courier New', Courier, monospace;
	}

	.header {
		padding: 20px 10px;
		margin: 0 10px;
		border-radius: 20px;
		color: #646464;
		text-align: center;
		font-family: 'Courier New', Courier, monospace;
	}

	.header .name {
		font-size: 25px;
		/* text-align: center; */
	}

	.header .con {
		/* text-indent: 36px; */
		margin: 10px 0;
	}

	.content .item {
		margin: 10px 20px;
		padding: 10px;
		border: 1px dotted #C0C0C0;
		border-radius: 15px;
		background-color: #FFFFFF;
		box-shadow: 10px 10px 5px #888888;
	}

	.content .title {
		margin-bottom: 10px;
	}

	.content .option {
		border-bottom: 1px solid #dde5ed;
		border-radius: 5px;
		padding: 10rpx;
		margin-bottom: 10rpx;
		background-color: #fffaeb;
	}

	.content .option radio {
		margin: 0 10px;
	}

	.footer {
		margin: 30px 20px 50px 20px;
	}

	.limit {
		color: #C0C0C0;
		font-size: 16px;
	}

	.content .select {
		border-bottom: 1px solid #dde5ed;
		border-radius: 5px;
		padding: 10rpx;
		margin-bottom: 10rpx;
		background-color: #fecbad;
	}
	.iconfont {
		font-family: "iconfont" !important;
		font-size: 20px;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
	}
</style>
